package com.sumainfor.pc.gmio.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.entity.ParentDeptVo;
import com.sumainfor.common.entity.SysUserVo;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.HoForcedMapper;
import com.sumainfor.pc.gmio.dao.HoLivecardMapper;
import com.sumainfor.pc.gmio.dao.UtilsMapper;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.gmio.service.HoForcedService;
import com.sumainfor.pc.gmio.service.HoLivecardService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 服务管家强制退
 */
@Service
public class HoForcedSerivceImpl implements HoForcedService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(HoForcedSerivceImpl.class);
    @Autowired
    private HoForcedMapper hoForcedMapper;
    @Autowired
    private UtilsMapper utilsMapper;
    @Autowired
    private ZpcMktFeign zpcMktFeign;
    /**
     *查询强制退申请列表
     */
    public List<Map<String,Object>> getHoForcedList(Map<String,Object> params){
        List<Map<String, Object>> gmioList = hoForcedMapper.getHoForcedList(params);
        //b保存录入账单的服务秘书的编号
        List<String> LiIdList = new ArrayList<String>();
        for (Map<String, Object> map : gmioList) {
            if (map.get("HouInfoId") != null) {
                LiIdList.add(map.get("HouInfoId").toString());
            }
        }
        params.put("LiIdList",LiIdList);
        JsonResult result=zpcMktFeign.getHoulivecardList(params);
        List<Map<String,Object>> marketList=(List<Map<String,Object>>)result.get("result");
        for (Map<String,Object> marketMap: marketList){
            for (Map<String,Object> gmioMap : gmioList) {
                if (MapUtils.getString(marketMap, "HouInfoId").equals(MapUtils.getString(gmioMap, "HouInfoId"))) {
                    gmioMap.putAll(marketMap);
                }
            }
        }
        return gmioList;
    }
    /**
     * 查询强制退列表总数
     */
    public Integer getHoForcedListCount(Map<String,Object> params){
        return hoForcedMapper.getHoForcedListCount(params);
    }
    /**
     * 服务管家强制退申请
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addHoForce(Map<String, Object> params) {
        JsonResult result=new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                //获取当前操作人的服务中心编号
                SysUserVo userVo = ContextUtils.getSysUser();
                ParentDeptVo vo = null;
                for (int i = 0; i < userVo.getLength(); i++) {
                    ParentDeptVo vo1 = userVo.getParentDeptVo();
                    if (vo1.getType().equals(3)) {
                        vo = vo1;
                        break;
                    }
                }
                //强制退编号
                String UUID="";
                Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                    put("uuid", "ForId");
                    put("database", Constants.DATA_SOURCE_GMIO);
                    put("surface", "tcgmio_force");
                }});   if(uuid!=null &&(!uuid.get("uuid").toString().equals(""))){
                    int number=Integer.valueOf(uuid.get("uuid").toString());
                    UUID= ComUtils.getLocalTrmSeqNum("tf",number);
                }else{
                    UUID=ComUtils.getLocalTrmSeqNum("tf",0);
                }
                if (vo!=null){
                    params.put("MiId",vo.getDeptId());//服务中心编号
                }
                params.put("ForId",UUID);//强制退编号
                params.put("AppId",ContextUtils.getUserId());//强制退申请人
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("term", "2");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
                params.put("AppTime",params.get("instm"));//强制退申请时间

                if (hoForcedMapper.addHoForce(params)>0){
                    result.putSuccess("操作成功");
                }else{
                    result.putFailed("服务器繁忙");
                }
            } catch (Exception e) {
                result.putFailed("服务器繁忙");
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
}
